import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


public class ImageStorage {
	
	CharacterImage[] all = new CharacterImage[40];
	Game game;
	
	public ImageStorage(Game game){
		int i = 0;
		while(i < all.length)
		{
			all[i] = new CharacterImage();
			i++;
		}

		this.game = game;
	}
	public BufferedImage getImage(int character,int type,int img)
	{
		return all[character].get(type,img);
	}
	public void loadImages(){
		
		FileReader fr;
		try {
			String path = getClass().getClassLoader().getResource(".").getPath();
			fr = new FileReader(path + "\\LoadImages.txt");
			BufferedReader br = new BufferedReader(fr);
			String str = br.readLine();
			int character = Integer.parseInt("" + str.charAt(0) + str.charAt(1));
			int type = Integer.parseInt(str.charAt(3) + "");
			str = str.substring(5);
			int i = 0;
			while(str != null)
			{
				File file = new File(path + str);
				BufferedImage img = null;
				try {
					img = ImageIO.read(file);
				} catch (IOException e) {
					System.out.printf("image %s dne \n", path + str);
				}
				all[character].add(img, type, i);
				str = br.readLine();
				if(str != null)
				{
					character = Integer.parseInt("" + str.charAt(0) + str.charAt(1));
					int temp = Integer.parseInt(str.charAt(3) + "");
					if(temp == type)
					{
						i++;
					}
					else
					{
						i= 0;
					}
					type = temp;
					str = str.substring(5);
				}
			}
			br.close();
			fr.close();
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public BufferedImage getMap(int i)
	{
		String path = getClass().getClassLoader().getResource(".").getPath();
		try{
		File file = new File(path + "\\Maps\\map" + i + ".png");
		return ImageIO.read(file);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		return null;
	}
}
